#include "qqmusic.h"

#include <QHBoxLayout>
#include <QWidget>
#include <QVBoxLayout>

QQMusic* QQMusic::instance = nullptr;


QQMusic::QQMusic(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("QQ音乐");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    resize(800, 600);

    // 初始化主窗口
    initMainWindow();

    // 初始化左侧窗口布局
    initLeftWindow();
    // 初始化右侧窗口布局
    initRightWindow();


    // 初始化信号槽
    initSignalSlot();
}

QQMusic *QQMusic::getInstance()
{
    if(instance == nullptr)
    {
        instance = new QQMusic();
    }

    return instance;
}

QQMusic::~QQMusic()
{

}

void QQMusic::initMainWindow()
{
    //水平布局
    QHBoxLayout* layout = new QHBoxLayout();
    //setSpacing 让间隔变为0
    layout->setSpacing(0);
    // 让layout 周围紧挨着
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);

    windowLeft = new QWidget();
    windowRight = new QWidget();

    windowLeft->setFixedWidth(150);
    windowRight->setMinimumWidth(800);

    windowLeft->setStyleSheet("QWidget {background-color: rgb(230,230,230);}");
    windowRight->setStyleSheet("QWidget {background-color: rgb(240,240,240);}");

    layout->addWidget(windowLeft);
    layout->addWidget(windowRight);
}

void QQMusic::initLeftWindow()
{
    // 创建垂直布局，设置控件间隔和外边距
    QVBoxLayout* layout = new QVBoxLayout();
    windowLeft->setLayout(layout);
    layout->setSpacing(10);  // 控件之间的间隔
    layout->setContentsMargins(10, 20, 10, 10);  // 边缘的间距

    // 第一个水平布局：头像和 "QQ音乐"
    QHBoxLayout *userAvatarLayout = new QHBoxLayout();

    // 头像按钮
    userAvatar = new QPushButton();
    userAvatar->setIconSize(QSize(45, 45));  // 更大一点的头像
    userAvatar->setFixedSize(45, 45);
    userAvatar->setIcon(QIcon(":/resource/image/avatar.png"));
    userAvatar->setStyleSheet(
        "QPushButton {"
        "   background-color: transparent;"
        "   border-radius: 25px;"  // 使头像按钮圆形
        "}"
        );

    // "QQ音乐" 标签
    qqLabel = new QLabel("QQ音乐");
    qqLabel->setStyleSheet(
        "QLabel {"
        "   background-color: transparent;"
        "   color: #D3D3D3;"  // 更柔和的灰色
        "   font-size: 20px;"
        "   font-weight: bold;"
        "}"
        );

    // 添加头像和标签到水平布局
    userAvatarLayout->addWidget(userAvatar, 0, Qt::AlignTop | Qt::AlignLeft);  // 头像左对齐
    userAvatarLayout->addWidget(qqLabel, 1, Qt::AlignTop | Qt::AlignLeft);      // 标签左对齐

    // 第二个水平布局：推荐按钮和 "推荐" 标签
    QHBoxLayout* recommendLayout = new QHBoxLayout();

    recommendTabBtn = new QPushButton();  // 按钮显示文字
    recommendTabBtn->setText("推荐");
    recommendTabBtn->setIcon(QIcon(":/resource/image/recommend_inactive.png"));  // 设置图标
    recommendTabBtn->setIconSize(QSize(30, 30));  // 设置图标的大小
    recommendTabBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: transparent;"  // 背景透明
        "   border: none;"  // 移除按钮边框
        "   padding-top: 5px;"  // 上边距
        "   padding-bottom: 5px;"  // 下边距
        "   padding-right: 10px;"  // 给按钮右侧添加适当的间距
        "   padding-left: 45px;"  // 增加左边距，控制文字位置
        "   color: #D3D3D3;"  // 设置文字颜色为灰色
        "   font-size: 18px;"  // 设置字体大小
        "   font-weight: bold;"  // 设置字体加粗
        "   text-align: left;"  // 左对齐文字
        "}"
        );
    // 将按钮添加到布局中
    recommendLayout->addWidget(recommendTabBtn, 0,Qt::AlignLeft);


    QHBoxLayout* radioStationLayout = new QHBoxLayout();

    radioStationTabBtn = new QPushButton();
    radioStationTabBtn->setIconSize(QSize(30, 30));
    radioStationTabBtn->setFixedSize(30, 30);
    radioStationTabBtn->setIcon(QIcon(":/resource/image/radioStation.png"));
    radioStationTabBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: transparent;"
        "   border: none;"  // 移除按钮的边框
        "   padding: 5px;"   // 给按钮增加一些内边距
        "}"
        );


    radioStationLabel = new QLabel("电台");
    radioStationLabel->setStyleSheet(
        "QLabel {"
        "   background-color: transparent;"
        "   color: #D3D3D3;"  // 灰色
        "   font-size: 18px;"
        "   font-weight: bold;"
        "}"
        );

    // 将推荐按钮和标签添加到第二个水平布局
    radioStationLayout->addWidget(radioStationTabBtn, 0, Qt::AlignCenter);
    radioStationLayout->addWidget(radioStationLabel, 1, Qt::AlignCenter);


    QHBoxLayout* musicPavilionLayout = new QHBoxLayout();

    musicPavilionTabBtn = new QPushButton();
    musicPavilionTabBtn->setIconSize(QSize(30, 30));
    musicPavilionTabBtn->setFixedSize(30, 30);
    musicPavilionTabBtn->setIcon(QIcon(":/resource/image/radioStation.png"));
    musicPavilionTabBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: transparent;"
        "   border: none;"  // 移除按钮的边框
        "   padding: 5px;"   // 给按钮增加一些内边距
        "}"
        );


    musicPavilionLabel = new QLabel("音乐馆");
    musicPavilionLabel->setStyleSheet(
        "QLabel {"
        "   background-color: transparent;"
        "   color: #D3D3D3;"  // 灰色
        "   font-size: 18px;"
        "   font-weight: bold;"
        "}"
        );

    // 将推荐按钮和标签添加到第二个水平布局
    musicPavilionLayout->addWidget(musicPavilionTabBtn, 0, Qt::AlignCenter);
    musicPavilionLayout->addWidget(musicPavilionLabel, 1, Qt::AlignCenter);


    myMusicLabel = new QLabel();
    myMusicLabel->setText("我的音乐");
    myMusicLabel->setStyleSheet(
        "QLabel {"
        "   background-color: transparent;"
        "   color: #D3D3D3;"  // 灰色
        "   font-size: 18px;"
        "   font-weight: bold;"
        "}"
        );


    QHBoxLayout* myFavoriteLayout = new QHBoxLayout();

    myFavoriteBtn = new QPushButton();
    myFavoriteBtn->setIconSize(QSize(30, 30));
    myFavoriteBtn->setFixedSize(30, 30);
    myFavoriteBtn->setIcon(QIcon(":/resource/image/favorite_inactive.png"));
    myFavoriteBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: transparent;"
        "   border: none;"  // 移除按钮的边框
        "   padding: 5px;"   // 给按钮增加一些内边距
        "}"
        );


    myFavoriteLabel = new QLabel("我喜欢");
    myFavoriteLabel->setStyleSheet(
        "QLabel {"
        "   background-color: transparent;"
        "   color: #D3D3D3;"  // 灰色
        "   font-size: 18px;"
        "   font-weight: bold;"
        "}"
        );

    // 将推荐按钮和标签添加到第二个水平布局
    myFavoriteLayout->addWidget(myFavoriteBtn, 0, Qt::AlignCenter);
    myFavoriteLayout->addWidget(myFavoriteLabel, 1, Qt::AlignCenter);



    // 留出一段空间，让它好看
    QSpacerItem *spacer1 = new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Fixed);
    QSpacerItem *spacer2 = new QSpacerItem(10, 10, QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 将第一个和第二个布局添加到主垂直布局
    layout->addLayout(userAvatarLayout);
    layout->addItem(spacer1);
    layout->addLayout(recommendLayout);
    layout->addItem(spacer2);
    layout->addLayout(radioStationLayout);
    layout->addItem(spacer2);
    layout->addLayout(musicPavilionLayout);
    layout->addItem(spacer1);
    layout->addWidget(myMusicLabel);
    layout->addItem(spacer2);
    layout->addLayout(myFavoriteLayout);

    // 添加空白区域，将控件推到顶部
    layout->addStretch(1);
}

void QQMusic::initRightWindow()
{

}

void QQMusic::initSignalSlot()
{
    //设置槽函数，点击不同按钮使图片高亮
    connect(recommendTabBtn,&QPushButton::clicked,this,&QQMusic::switchTabSession);
}

void QQMusic::switchTabSession()
{
    // 获取当前按钮的状态
    QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());

    if (clickedButton == recommendTabBtn) {
        // 设置推荐按钮为高亮图标
        recommendTabBtn->setIcon(QIcon(":/resource/image/recommend_active.png"));
        // 其他按钮恢复默认图标
        // someOtherButton->setIcon(QIcon(":/resource/image/other_button.png"));
        // 可以继续为其他按钮添加类似的逻辑
    }
}

